/*
 * Copyright 2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zq.learn.springbatch.combination.batch;

import com.zq.learn.springbatch.mongo.StatisticsResult;
import com.zq.learn.springbatch.mongo.sourcedata.SourceData;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.data.MongoItemReader;
import org.springframework.batch.item.data.MongoItemWriter;
import org.springframework.batch.item.data.builder.MongoItemReaderBuilder;
import org.springframework.batch.item.data.builder.MongoItemWriterBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@EnableBatchProcessing
public class BatchConfig {
  @Autowired
  private MongoTemplate mongoTemplate;

  /**
   * 订单原数据Reader
   * @param status 订单状态：0-无效；1-有效；
   * @param day 订单日期
   * @param timestamp 任务时间戳（无实际作用，只为触发Job的多次执行）
   * @return
   */
	@Bean
  @StepScope
	public MongoItemReader<SourceData> orderItemReader(@Value("#{jobParameters[status]}") Long status,@Value("#{jobParameters[day]}") String day,@Value("#{jobParameters[timestamp]}")Long timestamp) {
    Map<String, Sort.Direction> sortOptions = new HashMap<>();
    sortOptions.put("price", Sort.Direction.DESC);
		return new MongoItemReaderBuilder<SourceData>()
				.name("orderReader")
				.collection("SourceData")
				.targetType(SourceData.class)
				.template(mongoTemplate)
				.jsonQuery("{status:{$eq:?0},'timestamp':{$regex:?1}}")
        .parameterValues(status,day)
        .pageSize(10)
        .sorts(sortOptions)
				.build();
	}

  /**
   * 订单统计数据Writer
   * @return
   */
	@Bean
	public MongoItemWriter<StatisticsResult> orderItemWriter() {
		return new MongoItemWriterBuilder<StatisticsResult>()
				.template(mongoTemplate)
				.collection("StatisticsResult")
				.build();
	}
}
